import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/res/lf_double_ext.dart';

import '../../../res/strings/lf_strings.dart';
import '../../../utils/lf_scroll_behavior.dart';
import '../../../widgets/common_failed_page.dart';
import '../../../widgets/user_card.dart';
import '../../lf_base_controller.dart';
import '../../lf_home/lf_home_logic.dart';
import '../../lf_profile/lf_profile_logic.dart';
import '../../lf_tab_contact/lf_contact_empty.dart';
import '../../lf_tab_contact/lf_contact_list_loading.dart';
import 'lf_search_logic.dart';

class LfSearchPage extends StatefulWidget {
  const LfSearchPage({super.key});

  @override
  State<LfSearchPage> createState() => _LfSearchPageState();
}

class _LfSearchPageState extends State<LfSearchPage> {
  final logic = Get.put(LfSearchLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: null,
      body: GetBuilder<LfSearchLogic>(
        builder: (logic) {
          if (logic.dataList.isEmpty) {
            if (logic.state == LoadingState.loading ||
                logic.state == LoadingState.init) {
              return const LfContactListLoading();
            } else if (logic.state == LoadingState.failed) {
              return CommonFailedPage(
                retry: () {
                  logic.getCustomParamHttpRequest(isRefresh: true);
                },
              );
            }
            return Container();
          }
          return Padding(
            padding: EdgeInsets.only(top: 10.h),
            child: ScrollConfiguration(
              behavior: LfScrollBehavior(),
              child: EasyRefresh(
                controller: logic.refreshController,
                onLoad: () async {
                  var result =
                      await logic.getCustomParamHttpRequest(isRefresh: false);
                  return result;
                },
                onRefresh: () async {
                  var result =
                      await logic.getCustomParamHttpRequest(isRefresh: true);
                  return result;
                },
                child: CustomScrollView(
                  slivers: [
                    SliverPadding(
                      padding: EdgeInsets.symmetric(horizontal: 10.w),
                      sliver: SliverList.separated(
                        itemBuilder: (context, index) {
                          var userEntity = logic.dataList[index];
                          return InkWell(
                            child: UserCardWidget(
                              userEntity: userEntity,
                              showNew: false,
                              needBlur: false,
                            ),
                            onTap: () {
                              toProfilePage(
                                  uid: userEntity.userId!,
                                  userEntity: userEntity);
                            },
                          );
                        },
                        separatorBuilder: (context, index) {
                          return 15.h.sbh;
                        },
                        itemCount: logic.dataList.length,
                      ),
                    )
                  ],
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
